var app=getApp();
var util=require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    NewList:[],
    containerShow:true,
    searchPanelShow:false,
    searchResult:[],
    lunbo:{},
    page:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (event) {
    var NewListUrl=app.globalData.it120Base +"/liujiahe/cms/news/list/v2?orderBy=newer&pageSize=10&page="+this.data.page;
    var lunboUrl=app.globalData.it120Base+"/liujiahe/cms/news/list/v2?orderBy=viewDown&pageSize=3";
    util.posthttp(NewListUrl,this.processClassificationList);
    util.posthttp(lunboUrl,this.lunboData);
    wx.showNavigationBarLoading({
      complete: (res) => {},
    })
  },
  lunboData:function(lunbodata){
    this.setData({lunbo:lunbodata.data.result});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var page=this.data.page++;
    page++;
    var NewListUrl=app.globalData.it120Base +"/liujiahe/cms/news/list/v2?orderBy=newer&pageSize=10&page="+page;
    console.log(NewListUrl);
    util.posthttp(NewListUrl,this.processClassificationList);
    // wx.showNavigationBarLoading({
    //   complete: (res) => {},
    // })
    this.setData({page:page++});
    wx.showNavigationBarLoading({
      complete: (res) => {},
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  processClassificationList:function(data){
    var NewList=[];
    console.log(data);
    for (var idx in data.data.result){
      var subject=data.data.result[idx];
      var title=subject.title;
      if(title.length>=15){
        title=title.substring(0,15)+"...";
      }
      var classification=subject.categoryId;
      switch (classification) {
        case 59311:
          classification="国内新闻";
          break;
        case 61384:
          classification="国际新闻";
          break;
        case 61385:
          classification="军事新闻";
          break;
        case 61386:
          classification="股票";
          break;
        case 61387:
          classification="基金";
          break;
        case 61388:
          classification="科技";
          break;
        case 61389:
          classification="英超";
          break;
        case 61390:
          classification="中超";
          break;
        case 61391:
          classification="医药";
          break;
        case 61392:
          classification="历史";
          break;
        case 61393:
          classification="佛学";
          break;
        case 59310:
          classification="娱乐";
          break;
      }
      var dateAdd=subject.dateAdd.split(" ")[0];
      var temp={
        title:title,
        classification:classification,
        NewListPic:subject.pic,
        NewListViews:subject.views,
        NewListId:subject.id,
        dateAdd:dateAdd,
        views:subject.views
      }
      NewList.push(temp);
    }
    var newnews=[];
    newnews=this.data.NewList.concat(NewList);
    this.setData({NewList:newnews});
    wx.hideNavigationBarLoading({
      complete: (res) => {},
    })
  },
  processSearchList:function(data){
    var searchResult=[];
    console.log(data);
    for (var idx in data.data.result){
      var subject=data.data.result[idx];
      var title=subject.title;
      if(title.length>=15){
        title=title.substring(0,15)+"...";
      }
      var classification=subject.categoryId;
      switch (classification) {
        case 59311:
          classification="国内新闻";
          break;
        case 61384:
          classification="国际新闻";
          break;
        case 61385:
          classification="军事新闻";
          break;
        case 61386:
          classification="股票";
          break;
        case 61387:
          classification="基金";
          break;
        case 61388:
          classification="科技";
          break;
        case 61389:
          classification="英超";
          break;
        case 61390:
          classification="中超";
          break;
        case 61391:
          classification="医药";
          break;
        case 61392:
          classification="历史";
          break;
        case 61393:
          classification="佛学";
          break;
        case 59310:
          classification="娱乐";
          break;
      }
      var dateAdd=subject.dateAdd.split(" ")[0];
      var temp={
        title:title,
        classification:classification,
        NewListPic:subject.pic,
        NewListViews:subject.views,
        NewListId:subject.id,
        dateAdd:dateAdd,
        views:subject.views
      }
      searchResult.push(temp);
    }
    this.setData({searchResult:searchResult});
    wx.hideNavigationBarLoading({
      complete: (res) => {},
    })
  },

  //获得文章编号 带参跳转 data-new-id自定义属性 P114
  onNewlistToNewid:function(event){
    var newId=event.currentTarget.dataset.newId;
    wx.setStorageSync('NewList', wx.getStorageSync('NewList')+","+newId);
    console.log(newId);
    wx.navigateTo({
      url: '../new-id/new-id?id='+newId,
    })
  },

  onMoreTap:function(event){
    // console.log(event);
    var category = event.currentTarget.dataset.category;
    wx.navigateTo({
      url: "more-movie/more-movie?category="+category,
    })
  },

  obBindFocus:function(event){//搜索input获得焦点
    this.setData({
      containerShow:false,
      searchPanelShow:true
    })
  },

  onCancelImgTap:function(event){//单击图标关闭
    this.setData({
      containerShow:true,
      searchPanelShow:false,
      searchResult:{},
      inputValue:""
    })
  },

  onBindConfirm:function(event){
    var keyWord=event.detail.value;
    var searchUrl=app.globalData.it120Base +"/liujiahe/cms/news/list/v2?titleLike="+keyWord;
    util.posthttp(searchUrl,this.processSearchList);
    wx.showNavigationBarLoading({
      complete: (res) => {},
    })
  }

})